package com.xinqi.modules.chat.chat.official.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.official.domain.ChatRemindConfigEntity;
import com.xinqi.modules.chat.chat.official.manager.ChatRemindConfigManager;
import com.xinqi.modules.chat.chat.official.mapper.ChatRemindConfigMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 新奇提醒通知设置表管理层实现
 *
 * @author: yyzhen
 * @date: 2023/05/05
 */
@Service
@RequiredArgsConstructor
public class ChatRemindConfigManagerImpl extends BaseServiceImpl<ChatRemindConfigMapper, ChatRemindConfigEntity> implements ChatRemindConfigManager {

    /**
     * 根据用户获取新奇提醒通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public ChatRemindConfigEntity selectByUserId(Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(userId), MessageUtils.message("user.id.not.black"));
        return this.baseMapper.selectOne(wrapper(userId));
    }

    /**
     * 根据用户获取新奇提醒通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public List<ChatRemindConfigEntity> selectByUserId(List<Long> userId) {
        LambdaQueryWrapper<ChatRemindConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ChatRemindConfigEntity::getUserId, userId);
        return this.baseMapper.selectList(wrapper);
    }

    /**
     * 初始化用户新奇提醒通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public boolean initByUserId(Long userId) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(userId), MessageUtils.message("user.id.not.black"));
        ChatRemindConfigEntity configEntity = this.baseMapper.selectOne(wrapper(userId));
        if (Objects.isNull(configEntity)) {
            //没有则初始化 -- 所有新奇提醒通知默认打开
            ChatRemindConfigEntity init = new ChatRemindConfigEntity();
            init.setUserId(userId);
            init.setIsDisturb(YesNoNumberEnum.NO.getCode());
            init.setComment(YesNoNumberEnum.YES.getCode());
            init.setGive(YesNoNumberEnum.YES.getCode());
            init.setReply(YesNoNumberEnum.YES.getCode());
            init.setJoinClass(YesNoNumberEnum.YES.getCode());
            init.setDropClass(YesNoNumberEnum.YES.getCode());
            init.setIdentityChange(YesNoNumberEnum.YES.getCode());
            init.setGroupChange(YesNoNumberEnum.YES.getCode());

            this.baseMapper.insert(init);
        }
         return Boolean.TRUE;
    }

    private LambdaQueryWrapper<ChatRemindConfigEntity> wrapper(Long userId) {
        LambdaQueryWrapper<ChatRemindConfigEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatRemindConfigEntity::getUserId, userId)
            .last("LIMIT 1");
        return wrapper;
    }

}
